<!DOCTYPE html>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<c:set var="contextPath" value="${pageContext.request.contextPath}" />
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>添加常用地址</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<meta content="width=device-width, initial-scale=1" name="viewport">
<link rel="stylesheet" type="text/css" href="${contextPath}/css/bootstrap.min.css" />
<link rel="stylesheet" type="text/css" href="${contextPath}/css/bootstrap-theme.min.css" />
<link rel="stylesheet" type="text/css" href="${contextPath}/css/bootstrapValidator.min.css" />
<link rel="stylesheet" type="text/css" href="${contextPath}/css/bootstrap-datetimepicker.min.css" />
<link rel="stylesheet" type="text/css" href="${contextPath}/css/public.css">
<script type="text/javascript" src="${contextPath}/js/jquery.min.js"></script>
<script type="text/javascript" src="${contextPath}/js/bootstrap.min.js"></script>
<script type="text/javascript" src="${contextPath}/js/common.js"></script>
<script type="text/javascript" src="${contextPath}/js/bootstrapValidator.min.js"></script>
<script type="text/javascript" src="${contextPath}/js/bootstrap-datetimepicker.min.js"></script>
<script type="text/javascript" src="${contextPath}/js/locales/bootstrap-datetimepicker.zh-CN.js"></script>
<body>
	<%@ include file="../nav.jsp"%>
	<div class="container">
		<div class="row">
			<!-- form: -->
			<section>
				<div class="col-lg-8 col-lg-offset-3">
			
					<form action="${contextPath}/security/addOrder" method="post" id="addOrder" name="addOrder" role="form" class="form-horizontal">
						<input type="text" class="hidden" name="customerId" id="customerId" value="${requestScope.customerId}">
						<div class="form-group">
							<!-- 客户名称 -->
							<label for="name" class="col-lg-3 control-label">
								客户名称：
								<span class="text-danger">*</span>
							</label>
							<div class="col-lg-4">
								<input type="text" class="form-control" readonly="readonly" name="name" id="name" value="${requestScope.name}" maxlength="30" placeholder="客户名称" autocomplete="off">
							</div>
						</div>
						<div class="form-group">
							<!-- 会员号 -->
							<label for="orderNumber" class="col-lg-3 control-label">
								订单号：
								<span class="text-danger">*</span>
							</label>
							<div class="col-lg-4">
								<input type="text" class="form-control" readonly="readonly" name="orderNumber" id="orderNumber" value="${requestScope.orderNumber}" placeholder="订单号" autocomplete="off">
							</div>
						</div>
						<div class="form-group">
							<!-- 地址 -->
							<label for="address" class="col-lg-3 control-label">
								送(发)货地址：
								<span class="text-danger">*</span>
							</label>
							<div class="col-lg-4">
								<input type="text" class="hidden" name="address" id="address" value="${requestScope.address}">
								<textarea onclick="openAddressModal('11');" rows="2" cols="2" name="addressText" id="addressText" placeholder="送(发)货地址" class="form-control">${requestScope.addressText}</textarea>
							</div>
						</div>
						<div class="form-group">
							<!-- 货站信息 -->
							<label for="freightstation" class="col-lg-3 control-label">货站信息：</label>
							<div class="col-lg-6">
								<div class="input-group">
									<input class="form-control" name="freightstation" type="text" placeholder="货站信息" autocomplete="off">
									<div class="input-group-btn">
										<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
											货站信息
											<span class="caret"></span>
										</button>
										<ul class="dropdown-menu pull-right" id="freightstation">
										</ul>
									</div>
									<!-- /btn-group -->
								</div>
							</div>
						</div>
						<div class="form-group">
							<!-- 货站会员号 -->
							<label for="freightstationcard" class="col-lg-3 control-label">会员号：</label>
							<div class="col-lg-4">
								<input type="text" class="form-control" name="freightstationcard" id="freightstationcard" value="${requestScope.freightstationcard}" maxlength="11" placeholder="会员号" autocomplete="off">
							</div>
						</div>
						<div class="form-group">
							<!-- 货款状态 -->
							<label for=paymentStatus class="col-lg-3 control-label">货款状态：</label>
							<div class="col-lg-3">
								<select class="form-control" id="paymentStatus" name="paymentStatus">
									<option value="欠款" selected="selected">欠款</option>
									<option value="全款">全款</option>
									<option value="免除">免除</option>
									<option value="坏账">坏账</option>
									<option value="代收">代收</option>
									<option value="已付">已付</option>
								</select>
							</div>
						</div>
						<div class="form-group">
							<!-- 已付金额 -->
							<label for="paid" class="col-lg-3 control-label">已付金额：</label>
							<div class="col-lg-3">
								<input class="form-control" id="paid" name="paid" type="text" placeholder="已付金额" autocomplete="off">
							</div>
						</div>
						<div class="form-group">
							<!-- 税率 -->
							<label for="tax" class="col-lg-3 control-label">税率：</label>
							<div class="col-lg-2">
								<div class="input-group">
									<input class="form-control" name="tax" type="text" placeholder="税率" value="0" autocomplete="off">
									<span class="input-group-addon">
										<strong>%</strong>
									</span>
								</div>
							</div>
						</div>
						<div class="form-group">
							<!-- 备注-->
							<label for="remark" class="col-lg-3 control-label">备注：</label>
							<div class="col-lg-4">
								<textarea rows="2" cols="2" name="remark" id="remark" class="form-control"></textarea>
							</div>
						</div>
						<div class="form-group">
							<div class="col-lg-9 col-lg-offset-3">
								<button type="button" class="btn btn-success" id="add">保存</button>
							</div>
						</div>
					</form>
				</div>
			</section>
			<!-- :form -->
		</div>
	</div>
	<!-- 地址模态框（Modal） -->
	<div class="modal fade" id="addressModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
		<div class="modal-dialog">
			<div class="modal-content">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
					<h4 class="modal-title">地址选择</h4>
				</div>
				<div class="table-responsive">
					<table class="table table-striped table-bordered table-hover">
						<thead>
							<tr>
								<th>序号</th>
								<th>默认</th>
								<th>联系人</th>
								<th>联系电话</th>
								<th>地址</th>
								<th>详细地址</th>
								<th>操作</th>
							</tr>
						</thead>
						<tbody id="address_show"></tbody>
					</table>
				</div>
				<ul class="pagination"></ul>
				<div class="modal-footer">
					<button type="button" class="btn btn-primary" onclick="addAddress()">添加地址</button>
					<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
				</div>
			</div>
			<!-- /.modal-content -->
		</div>
		<!-- /.modal -->
	</div>
	<script type="text/javascript">
		var timer;
		var winOpen;
		function IfWindowClosed() {
			if (winOpen.closed == true) {
				//alert("close");
				selectAddress(1);
				window.clearInterval(timer);
			}
		}
		$(document).ready(
				function() {
					$.ajax({
						type : 'POST',
						data : {},
						url : '${contextPath}/security/codeSelectProduct?codeKey=货站信息',
						dataType : 'json',
						success : function(data) {
							$.each(data.list, function(i, item) {
								$("#freightstation").append("<li><a href='javascript:selectValue(\"freightstation\",\"" + item.codeValue + "\");'>" + item.codeValue + "</a></li>");
							});

						}
					});
					selectValue = function(d, e) {
						var s=e.indexOf("@");
						if(s==-1){
							$("input[name=freightstation]").val(e);
							$("input[name=freightstationcard]").val("");
						}else{
							var d=e.substring(0,s);
							$("input[name=freightstation]").val(d);
							var c=e.substring(s+1);
							$("#freightstationcard").val(c);
						}
					}
					//=======================================================地址对话框
					addAddress = function() {
						var customerId = $("#customerId").val();
						var url = "${contextPath}/security/addAddress?customerId=" + customerId;
						winOpen = window.open(url, "添加常用地址", 'height=450,width=500,toolbar=no,menubar=no,scrollbars=no, resizable=no,location=no, status=no');
						timer = window.setInterval("IfWindowClosed()", 500);
					}
					openAddressModal = function(id) {
						var customerId = $("#customerId").val();
						$("#addressModal").modal('show');
						selectAddress(1);
					};

					selectFrequentlyAddress = function(id) {
						var customerId = $("#customerId").val();
						$.ajax({
							type : 'POST',
							data : {
								"id" : id,
								"customerId" : customerId
							},
							url : '${contextPath}/security/setDefAddress',
							dataType : 'json',
							success : function(data) {
								//alert(data.message);
								selectAddress(1);
							}
						});
					}
					selectCurrentOrderAddress = function(id) {
						$("#address").val(id);
						$.ajax({
							type : 'GET',
							data : {
								"id" : id
							},
							url : '${contextPath}/security/getAddressText',
							dataType : 'json',
							success : function(data) {
								$("#addressText").val(data.t);
								$("#addressModal").modal('hide');
							}
						});
					}
					deleteCurrentOrderAddress = function(id) {
						$.ajax({
							type : 'POST',
							data : {
								"id" : id
							},
							url : '${contextPath}/security/deleteAddress',
							dataType : 'json',
							success : function(data) {
								selectAddress(1);
								alert(data.message);
							}
						});
					}

					selectAddress = function(pageNum) {
						var customerId = $("#customerId").val();
						$.ajax({
							type : 'POST',
							data : {
								"page" : pageNum,
								"customerId" : customerId
							},
							url : '${contextPath}/security/address',
							dataType : 'json',
							success : function(data) {
								$("#address_show").empty();
								if (data.list == "") {
									$("#address_show").empty();
									$("#address_show").append("<tr class='text-danger'><td colspan='10'>未查询到相关数据</td></tr>");
									//alert("未查询到相关数据！");
								} else {
									$.each(data.list, function(i, item) {
										var checked = "";
										if (item.frequently == true) {
											checked = "checked=\"checked\"";
										}
										$("#address_show").append(
												"<tr>" + "<td>" + (i + 1) + "</td>" + "<td><input onclick='javascript:selectFrequentlyAddress(" + item.id + ");' type=\"checkbox\" " + checked
														+ "></input></td>" + "<td>" + item.name + "</td>" + "<td>" + item.telephone + "</td>" + "<td>" + item.address + "</td>" + "<td>"
														+ item.detailedAddress + "</td>" + "<td><a href='javascript:selectCurrentOrderAddress(" + item.id
														+ ")'>选择</a>|<a href='javascript:deleteCurrentOrderAddress(" + item.id + ")'>删除</a></td>" + "</tr>");

									});
									var pageDom = $(".pagination");
									pageDom.empty();
									pageTotal = data.totlePage;
									page.creatPageHTML(pageNum, pageTotal, pageDom, "selectAddress");
									/*--------------------------------------跳转页-------------------------------------------------------*/
									$(".pageGoBtn").click(function() {
										var pageNum = 0;
										pageNum = $(".pageGoText").val();
										if (pageNum > pageTotal) {
											pageNum = pageTotal;
										} else if (pageNum < 1) {
											pageNum = 1;
										}
										selectAddress(pageNum);
									});
								}
							}
						});
					}

					//=====================================================================
					$('.form_date').datetimepicker({
						language : 'zh-CN',
						linkField : "salesTime",
						endDate : new Date(),
						initialDate : new Date(),
						weekStart : 0,
						todayBtn : 1,
						autoclose : 1,
						startView : 2,
						minView : 2
					});
					$('.form_dater').datetimepicker('show');

					$("#close").click(function() {
						window.close();
					});
					//==================================================================================================================
					$('#addOrder').bootstrapValidator({
						live : 'enabled',
						submitButtons : '#add',
						message : 'This value is not valid',
						feedbackIcons : {
							valid : 'glyphicon glyphicon-ok',
							invalid : 'glyphicon glyphicon-remove',
							validating : 'glyphicon glyphicon-refresh'
						},
						fields : {
							orderNumber : {
								validators : {
									notEmpty : {
										message : '订单号不能为空'
									}
								}
							},
							paid : {
								validators : {
									numeric : {
										message : '只能输入数字'
									}
								}

							},
						}
					});

					//$('#addCustomer').bootstrapValidator();
					$('#add').click(function() {
						var bootstrapValidator = $('#addOrder').data('bootstrapValidator');
						//手动触发验证
						bootstrapValidator.validate();
						if (bootstrapValidator.isValid()) {
							//表单提交的方法、比如ajax提交
							addOrder.submit();
						}

					});
				});
	</script>
</body>
</html>

